package com.haoshuang.zhouzhoubang.ui.page.order.take.services.detail.meal

import android.annotation.SuppressLint
import android.content.ContentValues
import android.content.Intent
import android.graphics.Paint
import android.os.Bundle
import android.os.CountDownTimer
import android.util.Log
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.google.android.flexbox.JustifyContent
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ActivityServiceMealDetailsBinding
import com.haoshuang.zhouzhoubang.model.bean.ShoppingBannerBean
import com.haoshuang.zhouzhoubang.model.bean.SpreadMealDetail
import com.haoshuang.zhouzhoubang.ui.adapter.BzAdapter
import com.haoshuang.zhouzhoubang.ui.adapter.ImageAdapter3
import com.haoshuang.zhouzhoubang.ui.adapter.ServiceAdapter
import com.haoshuang.zhouzhoubang.ui.adapter.ServiceMealLabelAdapter
import com.lnkj.jialubao.utils.AccountUtils
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.setOnItemClick
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.visible
import java.text.DecimalFormat


/**
 *  分享服务  套餐详情
 */
class ServiceMealDetailsActivity :
    BaseVMActivity<ServiceMealDetailsViewModel, ActivityServiceMealDetailsBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        binding.apply {
            appBar.ivBack.clickWithTrigger { finish() }
            appBar.tvTitle.text = "服务详情"
        }
    }

    var list = arrayListOf<String>()
    var list2 = arrayListOf<ArrayList<String>>()
    var list3 = arrayListOf<String>()
    lateinit var adapter: ServiceAdapter
    lateinit var adapter2: ImageAdapter3
    lateinit var adapter3: BzAdapter
    lateinit var adapter4: ServiceMealLabelAdapter
    var ids = 0
    var listTime = arrayListOf<SpreadMealDetail.GuaranteeContent>()
    var bean: SpreadMealDetail? = null
    var stock = 0
    var share_image = ""
    var list8= arrayListOf<SpreadMealDetail.Attr.Data>()
    var price2="0"
    var price3="0"
    var price4="0"
    override fun initData() {
        ids = intent.getIntExtra("ids", 0)
        adapter = ServiceAdapter(list)
        adapter3 = BzAdapter(list3)
        adapter2 = ImageAdapter3(list2)
        adapter4 = ServiceMealLabelAdapter(list8)
        vm.getSpreadMealDetail("id" to ids, "area_id" to AccountUtils.user?.area_id)
        initBanner()
        var lay = LinearLayoutManager(this@ServiceMealDetailsActivity)
        var lay3 = LinearLayoutManager(this@ServiceMealDetailsActivity)
        lay3.orientation = LinearLayoutManager.HORIZONTAL
        lay.orientation = LinearLayoutManager.HORIZONTAL
        binding.apply {
            val flexboxLayoutManager1 = FlexboxLayoutManager(this@ServiceMealDetailsActivity)
            flexboxLayoutManager1.flexDirection = FlexDirection.ROW
            flexboxLayoutManager1.flexWrap = FlexWrap.WRAP   //按正常方向换行
            flexboxLayoutManager1.justifyContent = JustifyContent.FLEX_START //交叉轴的起点对齐。
            rvGg.layoutManager = flexboxLayoutManager1
            rvGg.adapter=adapter4
            adapter4.setOnItemClick { view, position,_ ->
                if (list8[position].isCheck==0){
                    for (b in list8){
                        b.isCheck=0
                    }
                    list8[position].isCheck=1
                    price2= list8[position].price
                    tvPrice.text = price2
                    tvPrice1.text = "¥" +  list8[position].cost
                }else{
                    price2=price3
                    tvPrice.text = price3
                    tvPrice1.text = "¥" + price4
                    list8[position].isCheck=0
                }
                adapter4.notifyDataSetChanged()
            }


            rvBz.layoutManager = lay3
            rvBz.adapter = adapter3

            llConfirm.clickWithTrigger {
                // 服务id
                val id = bean?.detail?.id
                // 服务名称
                val name = bean?.detail?.store_name
                // 服务规格
                val spec = list8.firstOrNull { it.isCheck == 1 }
                val suk = spec?.suk ?: ""
                // 服务价格
                val price = price2
                // 分类id
                val cate_id = ids.toString()
                val image = bean?.detail?.slider_image?.firstOrNull() ?: ""
                setResult(200, Intent().apply {
                    putExtra("id", id.toString())
                    putExtra("name", name)
                    putExtra("spec", suk)
                    putExtra("price", price)
                    putExtra("cate_id", cate_id)
                    putExtra("image", image)
                })
                finish()
            }

        }
    }

    private fun initBanner() {
        //设置xbanner的图片点击事件
        binding.banner.setOnItemClickListener { xBanner, model, view, position ->
            model as ShoppingBannerBean // 此处的model无法自动识别，因此需要手动将其转换成setBannerData的参数中的数据类型
            Log.e(ContentValues.TAG, "initBanner:setOnItemClickListener:${model} ")
        }
        //设置xbanner加载图片
        binding.banner.loadImage { xBanner, model, view, position ->
            model as ShoppingBannerBean // 此处的model无法自动识别，因此需要手动将其转换成setBannerData的参数中的数据类型
            //在此处使用图片加载框架Glide加载图片
            Glide.with(this)
                .load(model.url)

                .placeholder(R.mipmap.default_image)
                .error(R.drawable.ic_launcher_background)

                .into(view as ImageView)
        }

    }

    override fun startObserve() {
        vm.getSpreadMealDetailData.observeState(this) {
            onLoading { }
            onError { code, msg ->
                dismissLoading()
                if (code != "402") {
                    toast(msg)
                }
            }
            onSuccess {
                dismissLoading()
                if (it != null) {
                    binding.apply {
                        bean = it
                        var list5 = bean!!.detail.slider_image;
                        val bannerFreshData = mutableListOf<ShoppingBannerBean>()

                        for (i in list5.indices) bannerFreshData.add(
                            ShoppingBannerBean(
                                list5[i],
                                "商品图片${i}"
                            )
                        )

                        //刷新数据之后，需要重新设置是否支持自动轮播，通过判断图片数据列表的大小来决定
                        binding.banner.setAutoPlayAble(bannerFreshData.size > 1)
                        binding.banner.setBannerData(bannerFreshData)
                        try {
                            var bean=it.attr
                            tvName.text=bean.name
                            list8.addAll(bean.data)
                            list8[0].isCheck=1
                            adapter4.notifyDataSetChanged()
                        }catch (e:Exception){

                        }
                        price2=it.detail.price
                        price3=it.detail.price
                        price4=it.detail.base_price
                            vLine.visible()
                            storeName.text = it.detail.store_name
                            tvPrice.text = it.detail.price
                            tvPrice1.text = "¥" + it.detail.base_price
                            storeName2.text = it.detail.desc
                            tvPrice1.setPaintFlags(tvPrice1.getPaintFlags() or Paint.STRIKE_THRU_TEXT_FLAG)


                        list3.addAll(it.guarantee_title)
                        listTime = it.guarantee_content
                        adapter2.notifyDataSetChanged()
                        adapter3.notifyDataSetChanged()
                        list.clear()
                        if (it.detail.service_content != null) {
                            list.addAll(it.detail.service_content)
                        } else {
                            list.add("")
                        }
                        if (it.detail.description != null) {
                            list.addAll(it.detail.description)
                        } else {
                            list.add("")
                        }
                        if (it.detail.questions != null) {
                            list.addAll(it.detail.questions)
                        } else {
                            list.add("")
                        }
                        adapter.notifyDataSetChanged()
                    }
                }
            }
        }
    }

}